import { v1 } from "uuid";
import { PrismaClient } from "@prisma/client";
import { NextApiRequest, NextApiResponse } from "next";
import CryptoJS from "crypto-js";
import { get, pick } from "@/utils/_";
export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const prisma = new PrismaClient();
  const body = JSON.parse(req.body);
  const salt = v1().slice(-4);
  const dto = {
    name: get(body, "name"),
    account: get(body, "account"),
    password: CryptoJS.MD5(`${get(body, "password")}${salt}`).toString(),
    salt,
  };
  const user = await prisma.user.create({ data: dto });
  return res.json({
    data: pick(user, ["id", "name", "account", "createdAt"]),
  });
}
